package org.zjx.analysis.analyze.window.impl;

import lombok.Getter;
import org.zjx.analysis.analyze.window.AbstractWindowAnalyzer;
import org.zjx.analysis.identify.IdentifyFontUtil;
import org.zjx.analysis.util.TextBlockSplitter;
import org.zjx.command.entity.ParagraphBean;
import org.zjx.command.entity.Quest;
import org.zjx.command.enums.FontType;
import org.zjx.command.util.ImageUtil;
import org.zjx.window.window.IWindow;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.*;
import java.util.List;

@Getter
public class TaskBoxAnalysis extends AbstractWindowAnalyzer {
    //是否打开摄妖香提示
    private boolean isOpenSign;

    private Map<String, Quest> quests;

    public TaskBoxAnalysis(IWindow window) {
        super("任务弹窗识别", "采集", window);
    }


    private boolean analysisOpenSign() {
        if (!isOpened()) this.isOpenSign = false;
        List<ParagraphBean.Word> content = identifyFont(areaMap.get("摄妖香提示").getRectangle(), Collections.singletonList(Color.WHITE), FontType.NPC, 0, false);
        return false;
    }

    @Override
    protected void initResources() {
        areaMap.put("摄妖香提示", new ResolutionArea(ImageUtil.createRect(0, 0, 159, 29)));
        areaMap.put("闹钟", new ResolutionArea(ImageUtil.createRect(182, 1, 200, 21)));
        areaMap.put("长任务区域", new ResolutionArea(ImageUtil.createRect(175, 28, 312, 198)));
        areaMap.put("短任务区域", new ResolutionArea(ImageUtil.createRect(175, 28, 334, 198)));
        areaMap.put("打开", new ResolutionArea(ImageUtil.createRect(321, 4, 333, 21)));
        areaMap.put("关闭", new ResolutionArea(ImageUtil.createRect(319, 4, 330, 21)));
    }

    @Override
    public Rectangle closeArea() {
        return ImageUtil.createRect(319, 4, 330, 21);
    }

    @Override
    public Rectangle getOriginBounds() {
        return ImageUtil.createRect(464, 110, 798, 308);
    }

    @Override
    public Rectangle getRefImageArea() {
        return ImageUtil.createRect(234, 5, 288, 18);
    }

    @Override
    public BufferedImage getRefImage() {
        return ImageUtil.createImage("iVBORw0KGgoAAAANSUhEUgAAADcAAAAOCAYAAACLroQjAAAGoklEQVR4Xo1WaXfiS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PZEFIxrMf0Q5e0Bv8Rqb3SNMHeOHXP7wK2b1hLHsHOlIoJO4dcSOzDklf9fBX1usfsD2+4f70B1ar91yOo/kzOuMNbvX6D//oDXboUmVp+x3/BfaGeHEDPazhAAAAAElFTkSuQmCC");
    }

    public List<Quest> identifyTask(BufferedImage bufferedImage) {
        List<BufferedImage> bufferedImages = cutting(bufferedImage);
        List<Quest> quests = new ArrayList<>();
        for (BufferedImage image : bufferedImages) {
            quests.add(identifyQuest(image));
        }
        return quests;
    }

    public List<BufferedImage> cutting(BufferedImage bufferedImage) {
        List<Rectangle> splits = TextBlockSplitter.splitTextBlocks(bufferedImage);
        List<BufferedImage> bufferedImages = new ArrayList<>();
        for (Rectangle split : splits) {
            if (split.getWidth() < 100 || split.getHeight() < 25) continue;
            bufferedImages.add(ImageUtil.cutImage(bufferedImage, split));
        }
        return bufferedImages;
    }

    public Quest identifyQuest(BufferedImage bufferedImage) {
        List<ParagraphBean.Word> wordList = IdentifyFontUtil.identify(bufferedImage, Arrays.asList(TextBlockSplitter.TARGET_COLORS), IdentifyFontUtil.getFontAttributes(FontType.NPC), 14, false);
        Quest quest = new Quest();
        if (wordList != null) {
            quest.setName(wordList.get(0).getName());
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 1; i < wordList.size(); i++) {
                stringBuilder.append(wordList.get(i).getName());
            }
            quest.setContent(stringBuilder.toString());
        }
        return quest;
    }

    private boolean isContentLong() {
        Rectangle difRect = ImageUtil.createRect(313, 28, 334, 198);
        BufferedImage bufferedImage = ImageUtil.cutImage(screenshot, conversion(windowArea, difRect));
        return !ImageUtil.haveColor(bufferedImage, Collections.singletonList(new Color(255, 255, 255)));
    }

    @Override
    public void analyze(BufferedImage image) {
        this.isOpenSign = analysisOpenSign();
        Rectangle questRectangle = isContentLong() ? conversion(windowArea, areaMap.get("长任务区域").getRectangle()) : conversion(windowArea, areaMap.get("短任务区域").getRectangle());
        List<Quest> questList = identifyTask(ImageUtil.cutImage(screenshot, questRectangle));
        quests = new HashMap<>();
        for (Quest quest : questList) {
            quests.put(quest.getName(), quest);
        }
    }
}
